package com.xy.tianyu.module.picture.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.rxLifeScope
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.adapter.PicCategoryPagerAdapter
import com.xy.tianyu.module.picture.bean.PicMoreCategoryBean
import com.xy.tianyu.module.picture.bean.S
import com.xy.tianyu.network.Api
import kotlinx.android.synthetic.main.fragment_pic_more_category.*
import rxhttp.RxHttp
import rxhttp.toClass


/**
 * @time 2020/10/12 17:58
 * @class 描述:
 */
class PicMoreCategoryFragment :LazyLoadBaseFragment() {
    private val picCateFragmentList:MutableList<Fragment> = mutableListOf()
    private val picCateTitleList:MutableList<String> = mutableListOf()
    override fun setContentView(): Int {
       return R.layout.fragment_pic_more_category
    }

    override fun lazyLoad() {


        rxLifeScope.launch {
            val picMoreCategoryBean = getCategory("home")
            val sList = picMoreCategoryBean.s
            if (!sList.isNullOrEmpty()){
                val homeBean = S(
                    select = true,
                    g = true,
                    h = 1080,
                    id = "home",
                    k = "热门",
                    o = "",
                    p = 123456,
                    r = 0,
                    s = null,
                    t = "热门",
                    u = "",
                    w = 1280
                )
                sList.add(0,homeBean)
                for (i in sList.indices){
                    val s = sList[i]
                    picCateTitleList.add(s.t)
                    val bundle = Bundle()
                    bundle.putParcelable("category",s)
                    if (i == 0){
                        val picCategoryHotFragment =
                            PicCategoryHotFragment()
                        picCategoryHotFragment.arguments = bundle
                        picCateFragmentList.add(picCategoryHotFragment)
                    }else{
                        val picCategoryChildFragment =
                            PicCategoryChildFragment()
                        picCategoryChildFragment.arguments = bundle
                        picCateFragmentList.add(picCategoryChildFragment)
                    }
                }

                val pagerAdapter = PicCategoryPagerAdapter(childFragmentManager,picCateTitleList,picCateFragmentList)
                vp_cg_detail.adapter = pagerAdapter

                tab_cg_type.setupWithViewPager(vp_cg_detail)
            }
        }
    }

    override fun stopLoad() {

    }

    private suspend fun getCategory(cate:String):PicMoreCategoryBean{
        return RxHttp.get(Api.DAY_DAY_SEARCH_PICTURE_CATEGORIES_URL+cate)
            .toClass<PicMoreCategoryBean>()
            .await()
    }

}